//
//  VipXianShiView.m
//  AILove
//
//  Created by 甘世清 on 2024/1/7.
//

#import "VipXianShiView.h"
#import <RMStore.h>
@implementation VipXianShiView

- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.clearColor;
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView* visualView = [[UIVisualEffectView alloc]initWithEffect:effect];
    visualView.frame =CGRectMake((SCREEN_WIDETH-295)/2.f, (SCREEN_HEIGHT-210)/2.f, 295, 210);
    visualView.layer.cornerRadius = 12;
    visualView.layer.masksToBounds = YES;
    visualView.alpha = 0.90;
    [self addSubview:visualView];
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.clearColor;// SQHexColor(@"#FFFFFF", 0.24);
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(295);
        make.height.mas_equalTo(210);
        make.centerX.centerY.equalTo(self);
    }];
    UILabel * oneLabel= [[UILabel alloc]init];
    oneLabel.text = SQNSLocalString(@"新人限时特惠");
    oneLabel.textColor = SQColor(@"#FFFFFF");
    oneLabel.font = FONT_BOLD(18);
    [bgView addSubview:oneLabel];
    [oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView).offset(20);
        make.centerX.equalTo(bgView);
        make.height.mas_equalTo(18);
    }];
    _guanbi = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(15, 15) midmargin:0];
    _guanbi.image = SQGetImage(@"退出图标");
    [self addSubview:self.guanbi];
    [self.guanbi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(bgView.mas_bottom);
        make.size.mas_equalTo(45);
    }];
    
    YYLabel * label3 = [[YYLabel alloc]initWithFrame:CGRectMake(24, 62, 295-48, 72)];
    label3.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    label3.textAlignment = NSTextAlignmentCenter;
    label3.numberOfLines = 0;
    [bgView addSubview:label3];
    NSDictionary *attributes = @{NSFontAttributeName:FONT_MEDIUM(16) , NSForegroundColorAttributeName: SQColor(@"#FFFFFF")};
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:SQNSLocalString(@"新用户享1元7天VIP特权体验卡") attributes:attributes];
    
    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:SQNSLocalString(@"1元7天VIP")] color:SQColor(@"#F9DB8F") backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
    }];
   
    
    label3.attributedText = text;
    
    HeaderView * btn = [[HeaderView alloc]init];
    btn.font = FONT_MEDIUM(16);
    btn.text = SQNSLocalString(@"开通1元福利");
    btn.layer.cornerRadius = 4;
    btn.headerImage.image = SQGetImage(@"按钮背景");
    [btn.btn addTarget:self action:@selector(SQ_buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(-20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(263);
        make.centerX.equalTo(bgView);
    }];
    [self SQ_Header];
}

-(void)SQ_Header{
    [HttpTool post:Url22 paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model = [VipModel mj_objectWithKeyValues:httpModel.data];
        [self SQ_RMStore];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_RMStore{
    [[RMStore defaultStore] requestProducts:[NSSet setWithArray:@[self.model.appleId]] success:^(NSArray *products, NSArray *invalidProductIdentifiers) {
       if([products count] == 0){
           DTLog(@"--------------没有商品------------------");
           return;
       }
       DTLog(@"内购商品列表------%@",products);
   } failure:^(NSError *error) {
       DTLog(@"--------------请求商品失败--------------%@----",error);
   }];
}

-(void)SQ_buttonClick{
    //调用内购
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    WS(weakSelf);
     [[RMStore defaultStore] addPayment:self.model.appleId success:^(SKPaymentTransaction *transaction) {
            [weakSelf checkIAPResultWithtransactionReceiptblock];
        } failure:^(SKPaymentTransaction *transaction, NSError *error) {
            [YJProgressHUD showMsgWithoutView: SQNSLocalString(@"交易失败")];
    }];
}

- (void)checkIAPResultWithtransactionReceiptblock
{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
        if (receiptURL) {
            NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
            DTLog(@"%@",receiptData);
            NSString *encodeStr = [receiptData base64EncodedStringWithOptions: NSDataBase64EncodingEndLineWithLineFeed];
             NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            [dic setObject:encodeStr forKey:@"certificate"];
             [YJProgressHUD SQ_showMsgWithoutView:@""];
            [HttpTool post:Url21  paramDic:dic successBlock:^(BaseModel * _Nullable httpModel) {
                UserModel * mo = [UserModel share];
                mo.isVip = YES;
                [UserModel updateLocalUserModel:mo];
                [YJProgressHUD showMsgWithoutView:@"购买成功"];
                for (SKPaymentTransaction *aTransaction in [[SKPaymentQueue defaultQueue] transactions]) {
                    [[SKPaymentQueue defaultQueue]finishTransaction:aTransaction];
                }
                if (self.VipBlock) {
                    self.VipBlock();
                }
            } andFailBloc:^(BaseModel * _Nullable httpModel, NSError * _Nullable error) {
                [YJProgressHUD showMsgWithoutView:httpModel.message];
            }];
        }else{
            [YJProgressHUD hide];
        }
}

@end
